//
//  NewLogViewController.m
//  iStore
//
//  Created by MacDev on 15/4/15.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "NewLogViewController.h"
#import "RegisterViewController.h"
#import "FindPasswordViewController.h"
#import "MyStoreViewController.h"
#import "RequestEngine.h"
#import "LogViewController.h"
#import "MBProgressHUD.h"
#import "Reachability.h"
#import "PersonalMessageViewController.h"


@interface NewLogViewController ()

{
    BOOL _flag;
    NSString  *_cooper;
    __weak IBOutlet UIButton *_button;
    
    __weak IBOutlet UIButton *_register;

    UIAlertView *alert;


}

@end

@implementation NewLogViewController
- (void)viewWillAppear:(BOOL)animated
{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(goBacksActions)];
    self.navigationController.navigationBar.hidden = NO;
    
    
   }

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading
    self.title = @"登录";
    _cooper = @"1";
    _flag = false;
    
    _userNameField.keyboardType = UIKeyboardTypeAlphabet;
    
    NSString *user = [[NSUserDefaults standardUserDefaults] objectForKey:kUserName];
    NSString *pass = [[NSUserDefaults standardUserDefaults] objectForKey:KPassword];
    if (user!=nil || ![user isEqualToString:@""] || pass!=nil || ![pass isEqualToString:@""] ) {
        
        _userNameField.text = user;
        _passWordField.text = pass;
    }
    [_button setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
    [_register  setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
    [findPassword  setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
#pragma mark --登录按钮事件--

// 提示按钮
- (IBAction)reminder:(UIButton *)sender {
    UIButton *btn = (UIButton *)[self.view viewWithTag:100];
    if (_flag) {
        [btn setImage:[UIImage imageNamed:@"Check-box_1.png"] forState:UIControlStateNormal];
        _cooper = @"1";
        _flag = !_flag;
        findPassword.enabled = YES;
        return;
    }
    [btn setImage:[UIImage imageNamed:@"Check-box"] forState:UIControlStateNormal];
    _flag = !_flag;
    _cooper = @"2";
    findPassword.enabled = NO;
    return;
}

- (IBAction)LoginButtonClick:(UIButton *)sender
{

    NSString *user = [_userNameField text];
    NSString *pass = [_passWordField text];
    
    if (user == nil || [user isEqualToString:@""] || pass == nil || [pass isEqualToString:@""]) {
        if (alert == nil) {
            alert = [[UIAlertView alloc] initWithTitle:@"登入错误" message:@"用户名密码不能为空" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        }
        [alert show];
        return;
    }
    
    [self activityStart];
    
    [RequestEngine userLoginWithDic:@{@"phone":user,
                                      @"password":pass,
                                      @"appKey":@"IOS",
                                      @"platformType":_cooper
                                      }
                          completed:^(NSString *errorcode,NSDictionary *dict) {
                              
                              
                              [self activityStop];
                              if ([errorcode isEqualToString:@"0"]) {
                                  
                                  //存储帐号密码用户信息
                                  [[NSUserDefaults standardUserDefaults] setObject:user forKey:kUserName];
                                  [[NSUserDefaults standardUserDefaults] synchronize];
                                  
                                  [[NSUserDefaults standardUserDefaults] setObject:dict[@"recommendCode"] forKey:kRecommendCode];
                                  [[NSUserDefaults standardUserDefaults] synchronize];
                                  
                                  [[NSUserDefaults standardUserDefaults] setObject:pass forKey:KPassword];
                                  [[NSUserDefaults standardUserDefaults] synchronize];
                                  
                                  [[NSUserDefaults standardUserDefaults] setObject:[dict objectForKey:@"userID"] forKey:kUserId];
                                  
                                  [[NSUserDefaults standardUserDefaults]setObject:[dict objectForKey:@"platformType"] forKey:KplatformType];
                                  [[NSUserDefaults standardUserDefaults] synchronize];
                                  
                                  
                                  
                                  UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc] init];
                                  returnButtonItem.title = @"返回";
                                  self.navigationItem.backBarButtonItem = returnButtonItem;
                                  if ([[NSString stringWithFormat:@"%@",dict[@"isInfoFilled"]] isEqualToString:@"0"]) {
                                      alert(@"请完善个人信息");
                                      PersonalMessageViewController *vc = [[PersonalMessageViewController alloc]init];
                                      [self.navigationController pushViewController:vc animated:YES];

                                  }
                                  else{
                                      MyStoreViewController * vc = [[MyStoreViewController alloc]init];
                                      UINavigationController * nAVC = [[UINavigationController alloc]initWithRootViewController:vc];
                                      [self presentViewController:nAVC animated:YES completion:nil];
                                  }
                              }else{
                                  if ([errorcode isEqualToString: @"-1"]) {
                                      alert(@"亲,你还没联网呢!!!");
                                      return;
                                  }
                                  
                                  NSString *result = [NSString stringWithFormat:@"%@",dict];
                                  alert(result);
                                  
                                  return;
                              
                              }
                              
                          }];


}
#pragma mark --注册按钮事件--
- (IBAction)registerButtonClick:(UIButton *)sender
{
//    sender.backgroundColor = KSelectedColor;
    RegisterViewController * vc = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --找回密码按钮事件--
- (IBAction)findPassWord:(UIButton *)sender
{
    
//    sender.backgroundColor = KSelectedColor;
    FindPasswordViewController * vc = [[FindPasswordViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)goBacksActions
{
    LogViewController *LVC = [[LogViewController alloc]init];
    [self.navigationController pushViewController:LVC animated:YES];


}
@end
